package net.melon.sysres.pojo;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.esj.basic.core.view.RequestUtils;
import net.esj.basic.pojo.AbstractPojo;
import net.esj.basic.utils.StringUtils;

public class SysFieldProperty extends AbstractPojo<Long> {

	private Long id;
	private String propertyId;
	private String url;
	private String model;
	private boolean add;
	private boolean edit;
	private boolean view;
	private boolean list;
	private boolean sort;
	private String defaults;
	private int editable;
	
	private Map<String, String[]> params = Collections.EMPTY_MAP;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getPropertyId() {
		return propertyId;
	}
	public void setPropertyId(String propertyId) {
		this.propertyId = propertyId;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public boolean isAdd() {
		return add;
	}
	public void setAdd(boolean add) {
		this.add = add;
	}
	public boolean isEdit() {
		return edit;
	}
	public void setEdit(boolean edit) {
		this.edit = edit;
	}
	public boolean isView() {
		return view;
	}
	public void setView(boolean view) {
		this.view = view;
	}
	public boolean isList() {
		return list;
	}
	public void setList(boolean list) {
		this.list = list;
	}
	public boolean isSort() {
		return sort;
	}
	public void setSort(boolean sort) {
		this.sort = sort;
	}
	public String getDefaults() {
		return defaults;
	}
	public void setDefaults(String defaults) {
		this.defaults = defaults;
	}
	public int getEditable() {
		return editable;
	}
	public void setEditable(int editable) {
		this.editable = editable;
	}
	
	public boolean canAlterAdd(){
		return (this.editable&0x10)>0;
	}
	
	public boolean canAlterEdit(){
		return (this.editable&0x8)>0;
	}
	
	public boolean canAlterView(){
		return (this.editable&0x04)>0;
	}
	
	public boolean canAlterList(){
		return (this.editable&0x02)>0;
	}
	
	public boolean canAlterSort(){
		return (this.editable&0x01)>0;
	}
	
	 public Map<String, String[]> params() {
		 if(this.params == Collections.EMPTY_MAP
				 &&StringUtils.hasText(url)){
			 this.params =  analyzeParams(url);
		 }
		return params;
	}
	 
	 private static final Map<String, String[]> analyzeParams(String url){
		if(url.indexOf("?")>0){
			String parStr = url.substring(url.indexOf("?")+1);
			Map<String, String[]> map = RequestUtils.parseQueryString(parStr);
			return map;
		}else{
			return Collections.EMPTY_MAP;
		}
	}
}
